
# 维护的是用户管理相关的接口
from api.base import Base
from loguru import logger

class UserManagerAPI(Base):

    # 初始化方法
    def __init__(self):
        self.add_user_url = self.get_url('/admin/admin/create')
        self.edit_user_url = self.get_url('/admin/admin/update')
        self.delete_user_url = self.get_url('/admin/admin/delete')
        self.search_user_url = self.get_url('/admin/admin/list?page={}&limit={}&sort={}&order={}')

        # self.login()        # 这是在做临时调试使用，后面会把这个去掉


    # 新增管理员
    def add_user(self,username,password,**kwargs):
        user_data = {"username":username,"password":password}
        if kwargs:
            logger.info("添加管理员的可选参数为:{}".format(kwargs))
            user_data.update(kwargs)
        return self.post(self.add_user_url,user_data)

    # 修改管理员
    def update_user(self,id,username,password,**kwargs):
        user_data = {'id':id,'username':username,'password':password}
        if kwargs:
            logger.info("修改管理员的可选参数为:{}".format(kwargs))
            user_data.update(kwargs)
        return self.post(self.edit_user_url,user_data)


    # 删除管理员
    def delete_user(self,id,username,**kwargs):
        user_data = {'id':id,'username':username}
        if kwargs:
            logger.info("删除管理员的可选参数为:{}".format(kwargs))
            user_data.update(kwargs)
        return self.post(self.delete_user_url,user_data)

    # 查询管理员
    def search_user(self,page=1,limit=20,sort='add_time',order='desc'):
        return self.get(self.search_user_url.format(page,limit,sort,order))


if __name__ == '__main__':

    user = UserManagerAPI()
    # res = user.add_user('xyz011','123456')
    # print(res)
    result = user.search_user()
    print(result)


